package com.gcb.common.utils;


import com.gcb.common.constant.HttpStatus;
import com.gcb.common.exception.ServiceException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class PasswordUtils {
    /**
     * 生成BCryptPasswordEncoder密码
     *
     * @param password 密码
     * @return 加密字符串
     */
    public static String encryptPassword(String password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(password);
        // TODO 观察一下后台登录是否正常
//        return DigestUtil.md5Hex(password);
    }

    /**
     * 判断密码是否相同
     *
     * @param rawPassword     真实密码
     * @param encodedPassword 加密后字符
     * @return 结果
     */
    public static boolean matchesPassword(String rawPassword, String encodedPassword) {
        // TODO 观察一下后台登录是否正常
         BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
         return passwordEncoder.matches(rawPassword, encodedPassword);
//        System.out.println(DigestUtil.md5Hex(rawPassword));
//        return DigestUtil.md5Hex(rawPassword).matches(encodedPassword);
    }

    /**
     * 获取app用户默认登入账号
     **/
    public static String getDefaultAppUserPassword(String mobile) {
        try {
            return PasswordUtils.encryptPassword(mobile.substring(mobile.length() - 6));
        } catch (Exception e) {
            throw new ServiceException("加密用户默认密码异常", HttpStatus.WARN);
        }
    }
}
